package com.rover022.tool.view.object
{
	import com.rover022.flixelgame.event.ItemEvent;
	import com.rover022.flixelgame.manager.ItemManager;
	import com.rover022.flixelgame.ui.equippackage.ItemBitMapMirror;
	import com.rover022.flixelgame.vo.ItemVo;
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import ghostcat.display.GBase;

	public class ItemUI extends GBase
	{
		public var itemVo:ItemVo;
		public var child:ItemBitMapMirror;

		public function ItemUI(skin:* = null, replace:Boolean = true)
		{
			super(skin, replace);
		}

		override public function set selected(v:Boolean):void
		{
			if (v)
			{
				this.alpha = 0.5;
			}
			else
			{
				this.alpha = 1;
			}
			super.selected = v;
			var event:ItemEvent = new ItemEvent(ItemEvent.ITEM_SELECT, true);
			dispatchEvent(event);
			//如果没有被选择
			if (!selected)
			{
				if(child){
					child.removeFromStage();
					child = null;
				}
			}
		}

		override public function get toolTip():*
		{
			var des:String = "";
			des += itemVo.name + "\n";
			des += itemVo.count + "\n";
			if (itemVo.compositeItems.length > 0)
			{
				des += "合成所指:" + "\n";
				var items:Array = itemVo.compositeItems;
				for (var i:int = 0; i < items.length; i++)
				{
					var array:Array = items[i];
					var makeId:String = array[0];
					var makeCount:int = array[1];
					var makeCan:Boolean = ItemManager.getInstance().canMakeItem(makeId);
					des += "可合成物品[" + ItemManager.getInstance().getItemFromConfig(makeId).name + "]  个数:" + makeCount + "\n";
					if (makeCan)
					{
						des += "(满足条件)";
					}
				}
			}
			else
			{
				des += "无可合成物品";
			}
			des += "\n--------------\n";
			des += "合成所需:" + "\n";
			if (itemVo.needCompositeItems.length > 0)
			{
				items = itemVo.needCompositeItems;
				for (var j:int = 0; j < items.length; j++)
				{
					array = items[j];
					makeId = array[0];
					makeCount = array[1];
					des += "合成所需[" + ItemManager.getInstance().getItemFromConfig(makeId).name + "]  个数:" + makeCount + "\n";
				}
			}
			else
			{
				des += "该物不能被合成";
			}
			return des;
		}

		/**
		 * 制作一个自己的映像
		 * @return
		 *
		 */
		public function getCopy():DisplayObject
		{
			child = new ItemBitMapMirror(this);
			child.addEventListener(MouseEvent.CLICK, childClick, false, 0, true);
			return child;
		}

		protected function childClick(event:MouseEvent):void
		{
			selected = !selected;
		}
	}
}
